﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Paxnam
{
    class Player : GameObject
    {

        public bool colidiu = false;

        public Direction previous_direction = Direction.STOPPED;
        public Direction actual_direction = Direction.RIGHT;

        int points;

        int lifes;

        public Vector2 hudPosition;

        public Color hudColor;

        animation chewingRight;
        animation chewingLeft;
        animation chewingUp;
        animation chewingDown;
        animation dying;

        //Direction previousDir;

        /// <summary>
        /// define se o player pegou o power up.
        /// </summary>
        private bool isPoweredUp;


        public int playerId = 0;

        /// <summary>
        /// Flag para indicar que o player está
        /// rodando a animação de morte e nenhum comando
        /// pode ser dado pelo jogador.
        /// </summary>
        private bool isDead;
        /*
         * TODO: Imaginar um jeito de mudar
         *      os bonus/atributos do personagem
         *      segundo as pilulas/bonus pegos
         */


        //acrescentar a animação

        public Player(GameWindow Window, Texture2D Texture)
        {
            this.Texture = Texture;
            this.Window = Window;
            this.Velocity = new Vector2(4, 4);
            this.Position = new Vector2(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2);
            this.points = 0;
            this.lifes = Game1.QTD_INICIAL_VIDAS;

            chewingLeft = new animation();
            chewingLeft.frameCount = 3;
            chewingLeft.frameHeight = 30;
            chewingLeft.frameRate = 1;
            chewingLeft.frameWidth = 30;
            chewingLeft.mapX = 0;
            chewingLeft.mapY = 0;

            chewingRight = new animation();
            chewingRight.frameCount = 3;
            chewingRight.frameHeight = 30;
            chewingRight.frameRate = 1;
            chewingRight.frameWidth = 30;
            chewingRight.mapX = 0;
            chewingRight.mapY = 30;

            chewingUp = new animation();
            chewingUp.frameCount = 3;
            chewingUp.frameHeight = 30;
            chewingUp.frameRate = 1;
            chewingUp.frameWidth = 30;
            chewingUp.mapX = 0;
            chewingUp.mapY = 60;

            chewingDown = new animation();
            chewingDown.frameCount = 3;
            chewingDown.frameHeight = 30;
            chewingDown.frameRate = 1;
            chewingDown.frameWidth = 30;
            chewingDown.mapX = 0;
            chewingDown.mapY = 90;

            dying = new animation();
            dying.frameCount = 11;
            dying.frameHeight = 30;
            dying.frameRate = 1;
            dying.frameWidth = 30;
            dying.mapX = 0;
            dying.mapY = 120;

            currentAnimation = chewingRight;
        }

        public override void Update()
        {
            
        }

        public void Update(GameTime gameTime, KeyboardState keyboardState)
        {
            if (playerId != 1) actual_direction = Direction.STOPPED;
            base.timer++;
            Window.Title = Convert.ToString(timer);
            if (timer >= 0)
            {
                timer = -10;
                //Window.Title = Convert.ToString(currentAnimation.currentFrame);
                currentAnimation.currentFrame = (currentAnimation.currentFrame < currentAnimation.frameCount-1 ? currentAnimation.currentFrame+=1 : 0);
            }

            if (!isDead)
            {
                if (playerId == 1)
                {
                    if (Game1.keyboardState.IsKeyDown(Keys.G) &&
                    !Game1.oldKeyboardState.IsKeyDown(Keys.G))
                    {

                    }

                    if (Game1.keyboardState.IsKeyDown(Keys.Right) &&
                                !Game1.oldKeyboardState.IsKeyDown(Keys.Right))
                    {
                        if (actual_direction != Direction.RIGHT)
                        {
                            // Aramaki
                            actual_direction = Direction.RIGHT;
                            currentAnimation = chewingRight;

                        }

                    }
                    else
                        if (Game1.keyboardState.IsKeyDown(Keys.Left) &&
                                !Game1.oldKeyboardState.IsKeyDown(Keys.Left))
                        {
                            if (actual_direction != Direction.LEFT)
                            {
                                // Aramaki
                                actual_direction = Direction.LEFT;
                                currentAnimation = chewingLeft;
                                
                            }

                        }
                        else
                            if (Game1.keyboardState.IsKeyDown(Keys.Down) &&
                                !Game1.oldKeyboardState.IsKeyDown(Keys.Down))
                            {
                                if (actual_direction != Direction.DOWN)
                                {
                                    // Aramaki
                                    actual_direction = Direction.DOWN;
                                    currentAnimation = chewingDown;
                                }
                            }
                            else
                                if (Game1.keyboardState.IsKeyDown(Keys.Up) &&
                                !Game1.oldKeyboardState.IsKeyDown(Keys.Up))
                                {
                                    if (actual_direction != Direction.UP)
                                    {
                                        // Aramaki
                                        actual_direction = Direction.UP;
                                        currentAnimation = chewingUp;

                                    }
                                }
                }
            }




            //if (!this.colidiu)
            //{
                switch (actual_direction)
                {
                    case Direction.LEFT:
                        //if (!this.colidiu && this.previous_direction != Direction.LEFT)
                            position.X -= Velocity.X;
                        break;
                    case Direction.RIGHT:
                        position.X += Velocity.X;
                        break;
                    case Direction.UP:
                        position.Y -= Velocity.Y;
                        break;
                    case Direction.DOWN:
                        position.Y += Velocity.Y;
                        break;

                }
            //}

            /*currentAnimation.currentFrame = (int)(gameTime.TotalGameTime.TotalSeconds *
                    currentAnimation.frameRate) % currentAnimation.frameCount;
             */
            //Window.Title = Convert.ToString(currentAnimation.currentFrame);
          

            if (currentAnimation.currentFrame == 10 && timer == -1)                                                        //Consegui solucionar a questão dos quadros colocando os comandos aqui no update do player...
            {

                Position = new Vector2(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2);

                if (lifes < 0)
                {
                    Game1.present_State = Game1.States.INTRO;
                    points = 0;
                    lifes = Game1.QTD_INICIAL_VIDAS;
                }

                currentAnimation = chewingRight;
                actual_direction = Direction.RIGHT;
                isDead = false;
            }

            #region collision with borders

            bool collidedWithBorders = false;



            if (position.X > Window.ClientBounds.Width - currentAnimation.frameWidth)
            {
                collidedWithBorders = true;
                position.X = Window.ClientBounds.Width - currentAnimation.frameWidth;
            }
            if (position.X < 0)
            {
                collidedWithBorders = true;
                position.X = 0;
            }
            if (position.Y < 0)
            {
                collidedWithBorders = true;
                position.Y = 0;
            }
            if (position.Y > Window.ClientBounds.Height - currentAnimation.frameHeight)
            {
                collidedWithBorders = true;
                position.Y = Window.ClientBounds.Height - currentAnimation.frameHeight;
            }

            if (collidedWithBorders && !isDead)
                Die();

            #endregion

            CollisionRect = new Rectangle((int)position.X, (int)position.Y, currentAnimation.frameWidth, currentAnimation.frameHeight);

        }

        public void Die()
        {
           if (!isDead)
            {

                isDead = true;
                lifes--;
                timer = -10;
                currentAnimation = dying;
                currentAnimation.currentFrame = 0;
                
            }
        }

        public new void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(Game1.arial20, "Vidas: " + lifes + "\nPontos: " + points, hudPosition, hudColor);

            //Aramaki
                switch (actual_direction)
                {
                case Direction.LEFT:
                     base.Draw(gameTime, spriteBatch, currentAnimation.currentFrame);
                     break;
                case Direction.RIGHT:
                     base.Draw(gameTime, spriteBatch, currentAnimation.currentFrame);
                    break;
                case Direction.UP:
                    base.Draw(gameTime, spriteBatch, currentAnimation.currentFrame);
                    break;
                case Direction.DOWN:
                    base.Draw(gameTime, spriteBatch, currentAnimation.currentFrame);
                    break;
                default:
                    base.Draw(gameTime, spriteBatch, currentAnimation.currentFrame);
                    break;
                
            }
        }

        public Rectangle CollisionRect
        {
            get
            {
                return new Rectangle((int)position.X, (int)position.Y, currentAnimation.frameWidth, currentAnimation.frameHeight);
            }
            set
            {
                base.CollisionRect = value;
            }
        }

        //public int SizeTexture()
        //{
        //    return currentAnimation.frameWidth * currentAnimation.frameHeight;
        //}

    }
}
